package com.knight.sychronized.lock;

import java.util.concurrent.locks.Lock;

public class ThreadA implements Runnable {

    private Lock lock;
    private Thread thread;

    public ThreadA(Lock lock) {
        this.lock = lock;
    }

    public ThreadA(Lock lock, Thread thread) {
        this.lock = lock;
        this.thread = thread;
    }

    @Override
    public void run() {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " 获取到锁,开始干活");
            Thread.sleep(4000L);
            System.out.println(Thread.currentThread().getName() + " 干活结束，准备释放锁");
            if (thread != null) {
                thread.interrupt();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();

        }

    }
}
